package com.liliudong.cursormate.handler;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.liliudong.cursormate.common.constant.CursorMetaConst;
import com.liliudong.cursormate.common.constant.SecurityConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * <a href="https://baomidou.com/pages/4c6bcf/">属性字段的自动注入</a>
 *
 * @author Liudong.li
 * @date 2023/10/25
 */
@Slf4j
@Component
public class CursorMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, CursorMetaConst.CREATED_AT, LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, CursorMetaConst.CREATED_BY, Long.class, StpUtil.getLoginIdAsLong());
        this.strictInsertFill(metaObject, CursorMetaConst.UPDATED_AT, LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, CursorMetaConst.UPDATED_BY, Long.class, StpUtil.getLoginIdAsLong());
        this.strictInsertFill(metaObject, CursorMetaConst.ORG_NO_BY, String.class, (String) StpUtil.getExtra(SecurityConst.ORG_NO));
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, CursorMetaConst.UPDATED_AT, LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, CursorMetaConst.UPDATED_BY, Long.class, StpUtil.getLoginIdAsLong());
    }
}